package itsm.isperp.module.controller.sparepart;

import itsm.isperp.framework.core.context.ContextHolder;
import itsm.isperp.framework.data.domain.DataRequest;
import itsm.isperp.framework.data.domain.PageResponse;
import itsm.isperp.framework.domain.IUser;
import itsm.isperp.framework.utils.HibernateUUIDGenerator;
import itsm.isperp.framework.web.controller.BaseProcessController;
import itsm.isperp.framework.web.request.JqGridRequest;
import itsm.isperp.framework.web.response.ResultMessage;
import itsm.isperp.module.entity.project.ProjProject;
import itsm.isperp.module.entity.sparepart.SptSparepartOut;
import itsm.isperp.module.service.app.AppDictService;
import itsm.isperp.module.service.app.AppUserService;
import itsm.isperp.module.service.project.ProjProjectService;
import itsm.isperp.module.service.sparepart.SptSparepartChangeDetailService;
import itsm.isperp.module.service.sparepart.SptSparepartOutService;
import itsm.isperp.workflow.service.BpmProcessRunService;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 备件管理控制器
 * 
 * @author lizx
 * @date 2013-9-12
 */
@Controller
@RequestMapping("sparepart/sparepartOut")
public class SptSparepartOutController extends
		BaseProcessController<SptSparepartOut> {

	@Autowired
	protected SptSparepartOutService sptSparepartOutService;

	@Autowired
	protected ProjProjectService projProjectService;

	@Override
	public SptSparepartOutService getService() {
		return sptSparepartOutService;
	}

	@Override
	public String getPageTitle() {
		return "备件管理";
	}

	@Override
	public String getProceessKey() {
		return "sparepartOutProcess";
	}

	@RequestMapping("edit")
	public ModelAndView edit(String id, HttpServletRequest req)
			throws Exception {
		ModelAndView mv = this.getModelAndView();

		SptSparepartOut model = null;

		AppUserService appUserService = ContextHolder
				.getSpringBean("appUserService");
		

		if (StringUtils.isEmpty(id)) {
			model = new SptSparepartOut();
			model.setId(HibernateUUIDGenerator.generate());
			IUser us=ContextHolder.getLoginUser();
			String username = us.getUsername();
			model.setCreator(username);
			model.setUseManId(username);
			model.setUseDate(new Date());
			mv.addObject("useManName", us.getFullname());
			mv.addObject("useManId", username);
		} else {
			model = this.getService().findOne(id);
			
			mv.addObject("useManName", model.getUseManName());
			mv.addObject("useManId", model.getUseManId());

		}

		mv.addObject("model", model);
		String projectId=req.getParameter("projectId");
		if(StringUtils.isNotEmpty(projectId))
		{
			List<ProjProject> list=new ArrayList<ProjProject>();
			list.add(projProjectService.findOne(projectId));
			mv.addObject("projects",list
					);
		}else
		{
			mv.addObject("projects",
					projProjectService.findAllPublished("id", "name"));
		}
		

		mv.addObject("creatorName",
				appUserService.findByUsername(model.getCreator()).getFullname());

		return mv;

	}

	@RequestMapping("delete")
	@ResponseBody
	@Override
	public ResultMessage delete(String id, HttpServletRequest req) {

		BpmProcessRunService bpmProcessRunService = ContextHolder
				.getSpringBean("bpmProcessRunService");
		String process_Id = (String) this.getService()
				.findOneToMap(id, "process_Id").get("process_Id");

		if (StringUtils.isNotEmpty(process_Id))
			bpmProcessRunService.deleteProcessInstance(process_Id, "删除",
					ContextHolder.getLoginUsername());

		SptSparepartChangeDetailService sptSparepartChangeDetailService = ContextHolder
				.getSpringBean("sptSparepartChangeDetailService");

		sptSparepartChangeDetailService.deleteByRelationId(id);

		this.getService().delete(id);

		return ResultMessage.newInstance(true);
	}

	@Override
	@RequestMapping(value = { "manage", "" })
	public ModelAndView manage(HttpServletRequest req) {
		ModelAndView mv = super.manage(req);

		// 项目
		DataRequest dataProject = new JqGridRequest();
		dataProject.isPageable(false);
		dataProject.setSelectColumns("id", "name");
		mv.addObject("projects", projProjectService.findAll(dataProject)
				.getContent());

		return mv;
	}

	@RequestMapping("outSptlist")
	@ResponseBody
	public PageResponse<Map<String, Object>> outSptlist(String sparepartId,
			JqGridRequest request, HttpServletRequest req) {
		return sptSparepartOutService.findAllSptDetailByRequest(sparepartId,
				request);
	}
	
	@RequestMapping("outStoreEdit")
	public ModelAndView outStoreEdit(String id, HttpServletRequest req)
			throws Exception {
		ModelAndView mv = this.getModelAndView();
		SptSparepartOut model = this.getService().findOne(id);
		mv.addObject("model", model);
		String projectId=model.getUseProjectId();
		if(StringUtils.isNotEmpty(projectId))
		{
			List<ProjProject> list=new ArrayList<ProjProject>();
			list.add(projProjectService.findOne(projectId));
			mv.addObject("projects",list
					);
		}else
		{
			mv.addObject("projects",
					projProjectService.findAllPublished("id", "name"));
		}
		return mv;

	}
	@RequestMapping("approveEdit")
	public ModelAndView approveEdit(String id, HttpServletRequest req)
			throws Exception {
		ModelAndView mv = this.getModelAndView();
		SptSparepartOut model = this.getService().findOne(id);
		mv.addObject("model", model);
		String projectId=model.getUseProjectId();
		if(StringUtils.isNotEmpty(projectId))
		{
			List<ProjProject> list=new ArrayList<ProjProject>();
			list.add(projProjectService.findOne(projectId));
			mv.addObject("projects",list
					);
		}else
		{
			mv.addObject("projects",
					projProjectService.findAllPublished("id", "name"));
		}
		return mv;

	}
	
	@RequestMapping("ciSelectorEdit")
	public ModelAndView ciSelectorEdit(String sourceId,String ty, HttpServletRequest req)
			throws Exception {
		ModelAndView mv = this.getModelAndView();
		SptSparepartOut model = this.getService().findOne(sourceId);
		mv.addObject("model", model);
		mv.addObject("sourceId", sourceId);
		mv.addObject("ty", ty);
		AppDictService appDictService = ContextHolder.getSpringBean("appDictService");
		mv.addObject("ciStatus", appDictService.findByType("CI_STATUS"));
		return mv;
	}
}
